/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormTPembelian.java
 *
 * Created on Feb 2, 2011, 4:40:55 PM
 */

package juwita;

import config.DBConnection;
import java.awt.CardLayout;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

//buat date
import java.util.Date;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.swing.table.TableColumn;

/**
 *
 * @author Abah
 */
public class FormTPembelian extends javax.swing.JInternalFrame {

    private DefaultTableModel DftTabModeTr;
    private DefaultTableModel DftTabModeBrg;    
    private DefaultTableModel DftTabModeSup;
    java.sql.Connection conn = new DBConnection().connect();
    
    int noItem = 0; //buat adddata    
    
    int[] aBal;
    int[] aSlop;
    int[] aBks;

    String sTglBeli = "";
    //bwat ngituh grandtotal harga
    Integer tHarga = 0;

    
    /** Creates new form FormTPembelian */
    public FormTPembelian() {
        initComponents();
        dcTglBeli.setDate(getDateTime());
        tbTransaksi.getTableHeader().setReorderingAllowed(false);
        Object[] Baris={"No","Kode","Nama Barang","Harga(Rp)","Bal","Slop","Bks","Total(Rp)"};
        DftTabModeTr=new DefaultTableModel(null,Baris);
        tbTransaksi.setModel(DftTabModeTr);

        aBal = new int[100];
        aSlop = new int[100];
        aBks = new int[100];

        setKdBeli();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        pnAtas = new javax.swing.JPanel();
        txtNamaBrg = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtKdBrg = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNamaSup = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtKdBeli = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtKdSup = new javax.swing.JTextField();
        btnCariBrg = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        txtSlop = new javax.swing.JTextField();
        txtHarga = new javax.swing.JTextField();
        btnCariSup = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        txtBal = new javax.swing.JTextField();
        txtBks = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        dcTglBeli = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAlamatSup = new javax.swing.JTextArea();
        jLabel16 = new javax.swing.JLabel();
        txtSubtotal = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        txtBalDB = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtSlopDB = new javax.swing.JTextField();
        txtBksDB = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        pnShow = new javax.swing.JPanel();
        pnTransaksi = new javax.swing.JPanel();
        spTransaksi = new javax.swing.JScrollPane();
        tbTransaksi = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        txtHargaAkhir = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnTrOk = new javax.swing.JButton();
        btnTrCancel = new javax.swing.JButton();
        btnTrNew = new javax.swing.JButton();
        pnBarang = new javax.swing.JPanel();
        pnCrBrg = new javax.swing.JPanel();
        spBarang = new javax.swing.JScrollPane();
        tbBarang = new javax.swing.JTable();
        pnAtas1 = new javax.swing.JPanel();
        btnShowAllBrg = new javax.swing.JButton();
        txtNmSrcBrg = new javax.swing.JTextField();
        cbSrcBrg = new javax.swing.JComboBox();
        btnSearchBrg = new javax.swing.JButton();
        lblCloseBrg = new javax.swing.JLabel();
        infoBrg = new javax.swing.JLabel();
        pnSup = new javax.swing.JPanel();
        pnCrSup = new javax.swing.JPanel();
        spSup = new javax.swing.JScrollPane();
        tbSup = new javax.swing.JTable();
        pnAtas2 = new javax.swing.JPanel();
        btnShowAllSup = new javax.swing.JButton();
        txtNmSrcSup = new javax.swing.JTextField();
        cbSrcSup = new javax.swing.JComboBox();
        btnSearchSup = new javax.swing.JButton();
        lblCloseSup = new javax.swing.JLabel();
        infoSup = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Transaksi Pembelian");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/pembelian.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setPreferredSize(new java.awt.Dimension(450, 737));
        pnTengah.setLayout(new java.awt.BorderLayout());

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        pnAtas.setPreferredSize(new java.awt.Dimension(450, 235));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNamaBrg.setEditable(false);
        pnAtas.add(txtNamaBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, 230, -1));

        jLabel2.setText("Kode Barang");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, -1, -1));

        txtKdBrg.setEditable(false);
        txtKdBrg.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtKdBrg.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtKdBrgKeyReleased(evt);
            }
        });
        pnAtas.add(txtKdBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 110, 20));

        jLabel3.setText("Tanggal Beli");
        pnAtas.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        txtNamaSup.setEditable(false);
        pnAtas.add(txtNamaSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 10, 160, -1));

        jLabel5.setText("Nama Barang");
        pnAtas.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));

        txtKdBeli.setEditable(false);
        txtKdBeli.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtKdBeli.setText("BL-0000001");
        pnAtas.add(txtKdBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 80, -1));

        jLabel7.setText("Alamat");
        pnAtas.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 30, -1, -1));

        jLabel8.setText("bal");
        pnAtas.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 160, -1, -1));

        txtKdSup.setEditable(false);
        pnAtas.add(txtKdSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 10, 60, -1));

        btnCariBrg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/search.png"))); // NOI18N
        btnCariBrg.setEnabled(false);
        btnCariBrg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariBrgActionPerformed(evt);
            }
        });
        pnAtas.add(btnCariBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 90, -1, 20));

        jLabel9.setText("bal");
        pnAtas.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 110, -1, -1));

        txtSlop.setForeground(new java.awt.Color(0, 0, 204));
        txtSlop.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtSlop.setText("0");
        txtSlop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSlopActionPerformed(evt);
            }
        });
        txtSlop.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSlopKeyReleased(evt);
            }
        });
        pnAtas.add(txtSlop, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 160, 40, -1));

        txtHarga.setEditable(false);
        txtHarga.setForeground(new java.awt.Color(0, 153, 0));
        txtHarga.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHarga.setText("0");
        pnAtas.add(txtHarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 130, 160, -1));

        btnCariSup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/search.png"))); // NOI18N
        btnCariSup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariSupActionPerformed(evt);
            }
        });
        pnAtas.add(btnCariSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 10, -1, 20));

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        pnAtas.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 150, 70, 30));

        txtBal.setForeground(new java.awt.Color(0, 0, 204));
        txtBal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtBal.setText("0");
        txtBal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBalKeyReleased(evt);
            }
        });
        pnAtas.add(txtBal, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, 40, -1));

        txtBks.setForeground(new java.awt.Color(0, 0, 204));
        txtBks.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtBks.setText("0");
        txtBks.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                txtBksMouseReleased(evt);
            }
        });
        txtBks.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBksKeyReleased(evt);
            }
        });
        pnAtas.add(txtBks, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 160, 40, -1));

        jLabel13.setText("slop");
        pnAtas.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 160, -1, -1));

        jLabel14.setText("bks");
        pnAtas.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 160, -1, -1));

        jLabel15.setText("Subtotal");
        pnAtas.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, -1, -1));

        dcTglBeli.setDateFormatString("dd/MM/yyyy");
        dcTglBeli.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dcTglBeliPropertyChange(evt);
            }
        });
        pnAtas.add(dcTglBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 140, -1));

        jLabel6.setText("Kode Beli");
        pnAtas.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        txtAlamatSup.setColumns(20);
        txtAlamatSup.setEditable(false);
        txtAlamatSup.setRows(5);
        jScrollPane1.setViewportView(txtAlamatSup);

        pnAtas.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 30, 220, 50));

        jLabel16.setText("Supplier");
        pnAtas.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 10, -1, -1));

        txtSubtotal.setEditable(false);
        txtSubtotal.setFont(new java.awt.Font("Tahoma", 1, 11));
        txtSubtotal.setForeground(new java.awt.Color(0, 153, 0));
        txtSubtotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtSubtotal.setText("0");
        txtSubtotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubtotalActionPerformed(evt);
            }
        });
        pnAtas.add(txtSubtotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 190, 160, -1));

        jLabel17.setText("Jumlah");
        pnAtas.add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

        txtBalDB.setEditable(false);
        txtBalDB.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtBalDB.setText("0");
        pnAtas.add(txtBalDB, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 110, 30, -1));

        jLabel10.setText("Harga");
        pnAtas.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, -1, -1));

        jLabel11.setText("bal");
        pnAtas.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 110, -1, -1));

        jLabel12.setText("slop");
        pnAtas.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 110, -1, -1));

        txtSlopDB.setEditable(false);
        txtSlopDB.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtSlopDB.setText("0");
        pnAtas.add(txtSlopDB, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 110, 30, -1));

        txtBksDB.setEditable(false);
        txtBksDB.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtBksDB.setText("0");
        pnAtas.add(txtBksDB, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 110, 30, -1));

        jLabel18.setText("bks");
        pnAtas.add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 110, -1, -1));

        jLabel20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/p_pembelian.png"))); // NOI18N
        pnAtas.add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 150, 80, 70));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        pnShow.setLayout(new java.awt.CardLayout());

        pnTransaksi.setLayout(new java.awt.BorderLayout());

        tbTransaksi.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Kode", "Nama Barang", "Harga (Rp)", "Jumlah", "Total(Rp)"
            }
        ));
        tbTransaksi.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbTransaksi.setGridColor(new java.awt.Color(0, 153, 0));
        tbTransaksi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbTransaksiMousePressed(evt);
            }
        });
        tbTransaksi.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbTransaksiKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbTransaksiKeyReleased(evt);
            }
        });
        spTransaksi.setViewportView(tbTransaksi);

        pnTransaksi.add(spTransaksi, java.awt.BorderLayout.CENTER);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(10, 80));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtHargaAkhir.setFont(new java.awt.Font("Tahoma", 1, 18));
        txtHargaAkhir.setForeground(new java.awt.Color(0, 153, 0));
        txtHargaAkhir.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHargaAkhir.setText("0");
        txtHargaAkhir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHargaAkhirActionPerformed(evt);
            }
        });
        jPanel1.add(txtHargaAkhir, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 10, 210, 30));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("Total Harga : ");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 10, 130, 20));

        btnTrOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/13.png"))); // NOI18N
        btnTrOk.setText("Ok");
        btnTrOk.setEnabled(false);
        btnTrOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrOkActionPerformed(evt);
            }
        });
        jPanel1.add(btnTrOk, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 80, 30));

        btnTrCancel.setText("Cancel");
        btnTrCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrCancelActionPerformed(evt);
            }
        });
        jPanel1.add(btnTrCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, 70, 30));

        btnTrNew.setText("New");
        btnTrNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrNewActionPerformed(evt);
            }
        });
        jPanel1.add(btnTrNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 3, 80, 30));

        pnTransaksi.add(jPanel1, java.awt.BorderLayout.PAGE_END);

        pnShow.add(pnTransaksi, "transaksi");

        pnBarang.setLayout(new java.awt.BorderLayout());

        pnCrBrg.setBackground(new java.awt.Color(255, 255, 255));
        pnCrBrg.setLayout(new java.awt.BorderLayout());

        tbBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbBarang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbBarangMousePressed(evt);
            }
        });
        tbBarang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbBarangKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbBarangKeyReleased(evt);
            }
        });
        spBarang.setViewportView(tbBarang);

        pnCrBrg.add(spBarang, java.awt.BorderLayout.CENTER);

        pnAtas1.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas1.setPreferredSize(new java.awt.Dimension(410, 70));
        pnAtas1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnShowAllBrg.setText("Show All");
        btnShowAllBrg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllBrgActionPerformed(evt);
            }
        });
        pnAtas1.add(btnShowAllBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 40, 80, -1));
        pnAtas1.add(txtNmSrcBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 270, -1));

        cbSrcBrg.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Brg", "Nama Brg" }));
        pnAtas1.add(cbSrcBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 80, -1));

        btnSearchBrg.setText("Search");
        btnSearchBrg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchBrgActionPerformed(evt);
            }
        });
        pnAtas1.add(btnSearchBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, 80, -1));

        lblCloseBrg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Delete.png"))); // NOI18N
        lblCloseBrg.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblCloseBrgMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                lblCloseBrgMouseReleased(evt);
            }
        });
        pnAtas1.add(lblCloseBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 10, 40, 40));

        infoBrg.setFont(new java.awt.Font("Tahoma", 1, 14));
        infoBrg.setForeground(new java.awt.Color(0, 0, 204));
        infoBrg.setText("Data Barang");
        pnAtas1.add(infoBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, -1, -1));

        pnCrBrg.add(pnAtas1, java.awt.BorderLayout.PAGE_START);

        pnBarang.add(pnCrBrg, java.awt.BorderLayout.CENTER);

        pnShow.add(pnBarang, "barang");

        pnSup.setLayout(new java.awt.BorderLayout());

        pnCrSup.setBackground(new java.awt.Color(255, 255, 255));
        pnCrSup.setLayout(new java.awt.BorderLayout());

        tbSup.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbSup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbSupMousePressed(evt);
            }
        });
        tbSup.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbSupKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbSupKeyReleased(evt);
            }
        });
        spSup.setViewportView(tbSup);

        pnCrSup.add(spSup, java.awt.BorderLayout.CENTER);

        pnAtas2.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas2.setPreferredSize(new java.awt.Dimension(410, 70));
        pnAtas2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnShowAllSup.setText("Show All");
        btnShowAllSup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllSupActionPerformed(evt);
            }
        });
        pnAtas2.add(btnShowAllSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 40, 80, -1));
        pnAtas2.add(txtNmSrcSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 270, -1));

        cbSrcSup.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Sup", "Nama Sup" }));
        pnAtas2.add(cbSrcSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 80, -1));

        btnSearchSup.setText("Search");
        btnSearchSup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchSupActionPerformed(evt);
            }
        });
        pnAtas2.add(btnSearchSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, 80, -1));

        lblCloseSup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Delete.png"))); // NOI18N
        lblCloseSup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblCloseSupMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                lblCloseSupMouseReleased(evt);
            }
        });
        pnAtas2.add(lblCloseSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 10, 40, 40));

        infoSup.setFont(new java.awt.Font("Tahoma", 1, 14));
        infoSup.setForeground(new java.awt.Color(255, 102, 0));
        infoSup.setText("Data Supplier");
        pnAtas2.add(infoSup, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, -1, -1));

        pnCrSup.add(pnAtas2, java.awt.BorderLayout.PAGE_START);

        pnSup.add(pnCrSup, java.awt.BorderLayout.CENTER);

        pnShow.add(pnSup, "supplier");

        pnTengah.add(pnShow, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 621, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnTengah, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 498, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(pnTengah, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(43, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Transaksi Pembelian?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isftPem = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void txtKdBrgKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKdBrgKeyReleased
        // TODO add your handling code here:
        txtKdBrg.setText(txtKdBrg.getText().toUpperCase());
}//GEN-LAST:event_txtKdBrgKeyReleased

    private void btnCariBrgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariBrgActionPerformed
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "barang");
        tampilSearchBrg();
}//GEN-LAST:event_btnCariBrgActionPerformed

    private void btnCariSupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariSupActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "supplier");
        tampilAllSup();
}//GEN-LAST:event_btnCariSupActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        addData();
}//GEN-LAST:event_btnAddActionPerformed

    private void tbTransaksiMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbTransaksiMousePressed
        //getDataFromBarang();
}//GEN-LAST:event_tbTransaksiMousePressed

    private void tbTransaksiKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbTransaksiKeyPressed
        // TODO add your handling code here:
}//GEN-LAST:event_tbTransaksiKeyPressed

    private void tbTransaksiKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbTransaksiKeyReleased
        // TODO add your handling code here:
        //getDataFromBarang();
    }//GEN-LAST:event_tbTransaksiKeyReleased

    private void btnTrOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrOkActionPerformed
        // TODO add your handling code here:
        insertDataBeli();
}//GEN-LAST:event_btnTrOkActionPerformed

    private void btnTrCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrCancelActionPerformed
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Batalkan input transaksi pembelian?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isftPen = false;
        }
}//GEN-LAST:event_btnTrCancelActionPerformed

    private void btnTrNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrNewActionPerformed
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Buat Transaksi baru?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            kosongkanNew();
        }
    }//GEN-LAST:event_btnTrNewActionPerformed

    private void tbBarangMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBarangMousePressed
        getDataFromBarang();
}//GEN-LAST:event_tbBarangMousePressed

    private void tbBarangKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBarangKeyPressed
        // TODO add your handling code here:
        getDataFromBarang();
}//GEN-LAST:event_tbBarangKeyPressed

    private void tbBarangKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBarangKeyReleased
        // TODO add your handling code here:
        getDataFromBarang();
}//GEN-LAST:event_tbBarangKeyReleased

    private void btnShowAllBrgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllBrgActionPerformed
        // TODO add your handling code here:
        tampilAllBrg();
}//GEN-LAST:event_btnShowAllBrgActionPerformed

    private void btnSearchBrgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchBrgActionPerformed
        // TODO add your handling code here:
        tampilSearchBrg();
}//GEN-LAST:event_btnSearchBrgActionPerformed

    private void lblCloseBrgMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCloseBrgMouseClicked
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "transaksi");
}//GEN-LAST:event_lblCloseBrgMouseClicked

    private void lblCloseBrgMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCloseBrgMouseReleased
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "transaksi");
}//GEN-LAST:event_lblCloseBrgMouseReleased

    private void tbSupMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbSupMousePressed
        // TODO add your handling code here:
        getDataFromSup();
}//GEN-LAST:event_tbSupMousePressed

    private void tbSupKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbSupKeyPressed
        // TODO add your handling code here:
        getDataFromSup();
}//GEN-LAST:event_tbSupKeyPressed

    private void tbSupKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbSupKeyReleased
        // TODO add your handling code here:
        getDataFromSup();
}//GEN-LAST:event_tbSupKeyReleased

    private void btnShowAllSupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllSupActionPerformed
        // TODO add your handling code here:
        tampilAllSup();
}//GEN-LAST:event_btnShowAllSupActionPerformed

    private void btnSearchSupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchSupActionPerformed
        // TODO add your handling code here:
        tampilSearchSup();
}//GEN-LAST:event_btnSearchSupActionPerformed

    private void lblCloseSupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCloseSupMouseClicked
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "transaksi");
}//GEN-LAST:event_lblCloseSupMouseClicked

    private void lblCloseSupMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCloseSupMouseReleased
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "transaksi");
}//GEN-LAST:event_lblCloseSupMouseReleased

    private void txtSubtotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubtotalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSubtotalActionPerformed

    private void txtHargaAkhirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHargaAkhirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHargaAkhirActionPerformed

    private void txtSlopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSlopActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_txtSlopActionPerformed

    private void txtSlopKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSlopKeyReleased
        // TODO add your handling code here:
        hitungTxtSubTotal();
    }//GEN-LAST:event_txtSlopKeyReleased

    private void txtBksMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBksMouseReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBksMouseReleased

    private void txtBksKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBksKeyReleased
        // TODO add your handling code here:
        hitungTxtSubTotal();
    }//GEN-LAST:event_txtBksKeyReleased

    private void txtBalKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBalKeyReleased
        // TODO add your handling code here:
        hitungTxtSubTotal();
    }//GEN-LAST:event_txtBalKeyReleased

    private void dcTglBeliPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dcTglBeliPropertyChange
        // TODO add your handling code here:
        if(dcTglBeli.getDate()!=null){
            //format yyyy-MM-dd utk mysql
            SimpleDateFormat formatTgl = new SimpleDateFormat("yyyy-MM-dd");
            sTglBeli= formatTgl.format(dcTglBeli.getDate());
        }
    }//GEN-LAST:event_dcTglBeliPropertyChange

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCariBrg;
    private javax.swing.JButton btnCariSup;
    private javax.swing.JButton btnSearchBrg;
    private javax.swing.JButton btnSearchSup;
    private javax.swing.JButton btnShowAllBrg;
    private javax.swing.JButton btnShowAllSup;
    private javax.swing.JButton btnTrCancel;
    private javax.swing.JButton btnTrNew;
    private javax.swing.JButton btnTrOk;
    private javax.swing.JComboBox cbSrcBrg;
    private javax.swing.JComboBox cbSrcSup;
    private com.toedter.calendar.JDateChooser dcTglBeli;
    private javax.swing.JLabel infoBrg;
    private javax.swing.JLabel infoSup;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCloseBrg;
    private javax.swing.JLabel lblCloseSup;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnAtas1;
    private javax.swing.JPanel pnAtas2;
    private javax.swing.JPanel pnBarang;
    private javax.swing.JPanel pnCrBrg;
    private javax.swing.JPanel pnCrSup;
    private javax.swing.JPanel pnShow;
    private javax.swing.JPanel pnSup;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JPanel pnTransaksi;
    private javax.swing.JScrollPane spBarang;
    private javax.swing.JScrollPane spSup;
    private javax.swing.JScrollPane spTransaksi;
    private javax.swing.JTable tbBarang;
    private javax.swing.JTable tbSup;
    private javax.swing.JTable tbTransaksi;
    private javax.swing.JTextArea txtAlamatSup;
    private javax.swing.JTextField txtBal;
    private javax.swing.JTextField txtBalDB;
    private javax.swing.JTextField txtBks;
    private javax.swing.JTextField txtBksDB;
    private javax.swing.JTextField txtHarga;
    private javax.swing.JTextField txtHargaAkhir;
    private javax.swing.JTextField txtKdBeli;
    private javax.swing.JTextField txtKdBrg;
    private javax.swing.JTextField txtKdSup;
    private javax.swing.JTextField txtNamaBrg;
    private javax.swing.JTextField txtNamaSup;
    private javax.swing.JTextField txtNmSrcBrg;
    private javax.swing.JTextField txtNmSrcSup;
    private javax.swing.JTextField txtSlop;
    private javax.swing.JTextField txtSlopDB;
    private javax.swing.JTextField txtSubtotal;
    // End of variables declaration//GEN-END:variables


    private void kosongkan(){        
        txtKdBrg.setText("");
        txtNamaBrg.setText("");        

        txtBal.setText("0");
        txtSlop.setText("0");
        txtBks.setText("0");
        txtBalDB.setText("0");
        txtSlopDB.setText("0");
        txtBksDB.setText("0");

        txtHarga.setText("0");

        txtSubtotal.setText(toRp(0));

    }
    private void kosongkanNew(){
        setKdBeli();

        tHarga = 0;

        txtKdBrg.setText("");
        txtNamaBrg.setText("");

        txtBal.setText("0");
        txtSlop.setText("0");
        txtBks.setText("0");

        txtBalDB.setText("0");
        txtSlopDB.setText("0");
        txtBksDB.setText("0");

        txtKdSup.setText("");
        txtNamaSup.setText("");
        txtAlamatSup.setText("");

        txtSubtotal.setText(toRp(0));
        txtHargaAkhir.setText(toRp(0));

        btnCariBrg.setEnabled(false);

        noItem = 0;
        
        //int rows = tbTransaksi.getRowCount();
        //System.out.println("rows : " + rows);
        
        DftTabModeTr.setNumRows(0);        
    }

    private void tampilSearchBrg(){
        /*
        String[] columnNames =
        {"No","Kode","Kota"};

        table = new  JTable(
                getDataSearch(), columnNames);
        spKota.setViewportView(table);
        */
        String srcby = "";
        if(cbSrcBrg.getSelectedItem().toString().equals("Kode Brg")){
            srcby = "kdBrg";
        }else{
            srcby = "nmBrg";
        }

        Object[] Baris={"No","Kode","Nama"};
        DftTabModeBrg=new DefaultTableModel(null,Baris);
        tbBarang.setModel(DftTabModeBrg);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg WHERE UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtNmSrcBrg.getText() + "%')");
            //int iNo = 0;
            while (rslt.next()) {
                int row = rslt.getRow();
                String sKdBrg = rslt.getString("kdBrg");
                String sNmBrg = rslt.getString("nmBrg");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {Integer.toString(row), sKdBrg, sNmBrg};
                DftTabModeBrg.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }
    private void tampilSearchSup(){
        String srcby = "";
        if(cbSrcBrg.getSelectedItem().toString().equals("Kode Sup")){
            srcby = "kdSup";
        }else{
            srcby = "nmSup";
        }

        Object[] Baris={"No","Kode","Nama"};
        DftTabModeSup=new DefaultTableModel(null,Baris);
        tbSup.setModel(DftTabModeSup);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tsup WHERE UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtNmSrcSup.getText() + "%')");
            //int iNo = 0;
            while (rslt.next()) {
                int row = rslt.getRow();
                String sKdSup = rslt.getString("kdSup");
                String sNmSup = rslt.getString("nmSup");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {Integer.toString(row), sKdSup, sNmSup};
                DftTabModeSup.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }
    private void tampilAllBrg(){

        String srcby = "";
        txtNmSrcBrg.setText("");

        Object[] Baris={"No","Kode","Nama"};
        DftTabModeBrg=new DefaultTableModel(null,Baris);
        tbBarang.setModel(DftTabModeBrg);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg");
            //int iNo = 0;
            while (rslt.next()) {
                int row = rslt.getRow();
                String sKdBrg = rslt.getString("kdBrg");
                String sNamaBrg = rslt.getString("nmBrg");

                String[] dataField = {Integer.toString(row), sKdBrg, sNamaBrg};
                DftTabModeBrg.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        // Disable auto resizing
        //tbBarang.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        // Set the first visible column to 100 pixels wide
        //int vColIndex = 0;
        //TableColumn col = tbBarang.getColumnModel().getColumn(vColIndex);
        //int width = 50;
        //col.setPreferredWidth(width);
    }
    private void tampilAllSup(){
        String srcby = "";
        txtNmSrcSup.setText("");

        Object[] Baris={"No","Kode","Nama"};
        DftTabModeSup=new DefaultTableModel(null,Baris);
        tbSup.setModel(DftTabModeSup);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tsup");
            //int iNo = 0;
            while (rslt.next()) {
                int row = rslt.getRow();
                String sKdSup = rslt.getString("kdSup");
                String sNamaSup = rslt.getString("nmSup");

                String[] dataField = {Integer.toString(row), sKdSup, sNamaSup};
                DftTabModeSup.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void deleteData(){
            try {
                int deleting;
                deleting = JOptionPane.showConfirmDialog(this,
                        "Apakah Anda yakin akan menghapus "

                        //+ cbKodeKota.getSelectedItem().toString() +" "
                        +txtKdBrg.getText() +"?",
                        "Konfirmasi",
                        JOptionPane.YES_NO_OPTION);
                if (deleting == 0){

                    PreparedStatement pStatement = null;
                    String sql = "DELETE FROM tbrg_gol " +
                            "WHERE kdGol=? ";
                    pStatement = conn.prepareStatement(sql);

                    pStatement.setString(1, txtKdBrg.getText());

                    int intTambah = pStatement.executeUpdate();
                    if (intTambah >0){
                        JOptionPane.showMessageDialog(this,
                                "Delete sukses", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);

                    }else {
                        JOptionPane.showMessageDialog(this,
                                "Delete gagal", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    pStatement.close();

                    kosongkan();
                    //kd/tampilData();
                }
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
    }
    private boolean isDuplicatePKey(){
        //Object oKdGol, oNamaGol;//, oKet;//, oTest;
        String pKey = txtKdBeli.getText();
        boolean result = true;
        Integer row = 0;

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbeli WHERE idBeli=" +
                    "'" +pKey+ "'");
            while (rslt.next()) {
                row = rslt.getRow();
            }
                if(row > 0){
                    //JOptionPane.showMessageDialog(this, "Kode "+pKey+" sudah ada"
                    //        ,"Peringatan",JOptionPane.WARNING_MESSAGE);
                    result = true;
                }else{
                    result = false;
                }
            System.out.println("row : " + row);
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        return result;
    }
    private void insertDataBeli(){        
        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbeli " +
                "(idBeli, tgl_beli, kdSup, total_harga, user_id) " +
                "VALUES (?,?,?,?,?)";
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, txtKdBeli.getText());
            //for date                                
            pStatement.setString(2, sTglBeli);
            
            pStatement.setString(3, txtKdSup.getText());
            //String kdKota = Integer.toString(cbTipeHarga.getSelectedIndex());
            //pStatement.setString(3, kdKota);
            pStatement.setInt(4, tHarga);
            pStatement.setString(5, "admin");

            int intTambah = pStatement.executeUpdate();
            if (intTambah>0){
                //JOptionPane.showMessageDialog(this,
                //        "Penambahan sukses", "Informasi",
                //        JOptionPane.INFORMATION_MESSAGE);

                //insert detail
                insertDataBeliDetail();
            }else{
                //JOptionPane.showMessageDialog(this,
                //        "Penambahan gagal", "Informasi",
                //        JOptionPane.INFORMATION_MESSAGE);
            }
            pStatement.close();
        }
        catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }

    }
    private void insertDataBeliDetail(){
        // panggil method koneksi
        //java.sql.Connection conn = new DBConnection().connect();

        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbeli_detail " +
                "(idBeli, kdBrg, hargaBeli, bal, slop, bks, subtotal) " +
                "VALUES (?,?,?, ?,?,?, ?)";

        int rows = tbTransaksi.getRowCount();
        for (int i=0; i< rows; i++) {
            try {
                pStatement = conn.prepareStatement(sql);
                //iHarga = iHarga + Integer.parseInt(tbTransaksi.getValueAt(i, 5).toString());
                pStatement.setString(1, txtKdBeli.getText());
                pStatement.setString(2, tbTransaksi.getValueAt(i, 1).toString());//kdbrg
                pStatement.setString(3, tbTransaksi.getValueAt(i, 3).toString());//harga
                
                //bal
                pStatement.setString(4, tbTransaksi.getValueAt(i, 4).toString());//bal
                //slop
                pStatement.setString(5, tbTransaksi.getValueAt(i, 5).toString());//slop
                //bks
                pStatement.setString(6, tbTransaksi.getValueAt(i, 6).toString());//bks
                //total
                pStatement.setString(7, tbTransaksi.getValueAt(i, 7).toString());//subtotal

                int intTambah = pStatement.executeUpdate();
                if (intTambah>0){
                    //JOptionPane.showMessageDialog(this,
                    //        "Penambahan data sukses", "Informasi",
                    //        JOptionPane.INFORMATION_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(this,
                            "Penambahan detail gagal", "Informasi",
                            JOptionPane.INFORMATION_MESSAGE);
                }
                pStatement.close();

            }catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
        }

        updateDataBrg();
    }

    private void updateDataBrg(){
        PreparedStatement pStatement = null;
        String sql = "UPDATE tbrg " +
                    "SET bal=?, slop=?, bks=?  " +
                    "WHERE kdBrg=? ";

        int rows = tbTransaksi.getRowCount();
        for (int i=0; i< rows; i++) {
            try {
                pStatement = conn.prepareStatement(sql);

                //bal
                pStatement.setInt(1, aBal[i+1] + Integer.parseInt(tbTransaksi.getValueAt(i, 4).toString()));//bal
                //slop
                pStatement.setInt(2, aSlop[i+1] + Integer.parseInt(tbTransaksi.getValueAt(i, 5).toString()));//slop
                //bks
                pStatement.setInt(3, aBks[i+1] + Integer.parseInt(tbTransaksi.getValueAt(i, 6).toString()));//bks

                pStatement.setString(4, tbTransaksi.getValueAt(i, 1).toString());//kdbrg
                System.out.println("kode barang : " + tbTransaksi.getValueAt(i, 1).toString());
                System.out.print("bal : " + aBal[i+1] + " + " + tbTransaksi.getValueAt(i, 4).toString());
                System.out.print(" slop : " + aSlop[i+1] + " + " + tbTransaksi.getValueAt(i, 5).toString());
                System.out.println(" bks : " + aBks[i+1] + " + " + tbTransaksi.getValueAt(i, 6).toString());
                int intTambah = pStatement.executeUpdate();                
            
                if (intTambah >0){
                    System.out.println("kode barang : " + tbTransaksi.getValueAt(i, 1).toString());
                    //JOptionPane.showMessageDialog(this,
                    //        "Update sukses", "Informasi",
                    //        JOptionPane.INFORMATION_MESSAGE);

                }else {
                    //JOptionPane.showMessageDialog(this,
                    //        "Edit gagal", "Informasi",
                    //        JOptionPane.INFORMATION_MESSAGE);
                }

                pStatement.close();
            
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
            
        }
        kosongkanNew();
    }   

    private void getDataFromBarang(){
                // TODO add your handling code here:
        Object oKdBrg, oNmBrg;//, oKet;//, oTest;
        Integer allStokBks=0, iBal=0, iSlop=0, iBks=0;

        oKdBrg = tbBarang.getValueAt(tbBarang.getSelectedRow(), 1);
        oNmBrg = tbBarang.getValueAt(tbBarang.getSelectedRow(), 2);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg WHERE kdBrg=" +
                    "'" +oKdBrg+ "'");
            while (rslt.next()) {
                txtHarga.setText(rslt.getString("hrgBeli"));
                iBal = rslt.getInt("bal");
                iSlop = rslt.getInt("slop");
                iBks = rslt.getInt("bks");
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        
        allStokBks = (iBal * 200) + (iSlop * 10) + iBks;

        txtBalDB.setText(iBal.toString());
        txtSlopDB.setText(iSlop.toString());
        txtBksDB.setText(iBks.toString());

        txtKdBrg.setText(oKdBrg.toString());
        txtNamaBrg.setText(oNmBrg.toString());        
    }
    private void getDataFromSup(){
                // TODO add your handling code here:
        Object oKdSup, oNmSup;//, oKet;//, oTest;

        oKdSup = tbSup.getValueAt(tbSup.getSelectedRow(), 1);
        oNmSup = tbSup.getValueAt(tbSup.getSelectedRow(), 2);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tsup WHERE kdSup=" +
                    "'" +oKdSup+ "'");
            while (rslt.next()) {                
                txtAlamatSup.setText(rslt.getString("alamat"));
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        txtKdSup.setText(oKdSup.toString());
        txtNamaSup.setText(oNmSup.toString());
        btnCariBrg.setEnabled(true); 
    }

    private void setKdBeli(){
        String idBeli = "";
        Integer noUrut = 0;
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbeli");
            while (rslt.next()) {
                noUrut = rslt.getRow();
                idBeli = rslt.getString("idBeli");
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        
        if(idBeli.equals("")){
            idBeli = "BL-0000000";
            idBeli = idBeli.substring(3);
            noUrut = Integer.parseInt(idBeli);
        }
        System.out.println(idBeli);           
        
        noUrut++;
        
        java.text.DecimalFormat nft = new java.text.DecimalFormat("#0000000.###");
        nft.setDecimalSeparatorAlwaysShown(false);
        
        txtKdBeli.setText("BL-"+nft.format(noUrut));
        while(isDuplicatePKey()){
            noUrut++;
            txtKdBeli.setText("BL-"+nft.format(noUrut));
        }
        System.out.println("BL-"+nft.format(noUrut));
        
    }

    private void addData(){
        //cek kdsupplier
        if(txtKdSup.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Data tidak valid, " +
                        "data supplier harus terisi","Error",
                    JOptionPane.ERROR_MESSAGE);
        //cek kdbarang
        }else if(txtKdBrg.getText().equals("")){
                JOptionPane.showMessageDialog(this, "Data tidak valid, " +
                        "data barang harus terisi","Error",
                    JOptionPane.ERROR_MESSAGE);
        }else{
            //cek input stok
            if(validateNumber(txtBal.getText()) &&
               validateNumber(txtSlop.getText())&&
               validateNumber(txtBks.getText())){

               System.out.println("Correct Number");
               Integer iBal = Integer.parseInt(txtBal.getText());
               Integer iSlop = Integer.parseInt(txtSlop.getText());
               Integer iBks = Integer.parseInt(txtBks.getText());
               Integer allStokInput = (iBal * 200) + (iSlop * 10) + (iBks);

               Integer iHarga = Integer.parseInt(txtHarga.getText());
               Integer subtotal =  allStokInput * iHarga;
               
                if(  iBal >= 1 ||
                     iSlop >= 1 ||
                     iBks >= 1 ){
                    //cek data sama                    
                    if(isSameKdBrg(txtKdBrg.getText())){
                        System.out.println("Sama!");                        
                        int rows = tbTransaksi.getRowCount();
                        for (int i=0; i< rows; i++) {
                        if(txtKdBrg.getText().equalsIgnoreCase(tbTransaksi.getValueAt(i, 1).toString())){
                            //update stok
                            iBal = iBal + Integer.parseInt(tbTransaksi.getValueAt(i, 4).toString());
                            tbTransaksi.setValueAt(iBal, i, 4);                            
                            iSlop = iSlop + Integer.parseInt(tbTransaksi.getValueAt(i, 5).toString());
                            tbTransaksi.setValueAt(iSlop, i, 5);
                            iBks = iBks + Integer.parseInt(tbTransaksi.getValueAt(i, 6).toString());
                            tbTransaksi.setValueAt(iBks, i, 6);
                            //update subtotal
                            subtotal = subtotal + Integer.parseInt(tbTransaksi.getValueAt(i, 7).toString());
                            tbTransaksi.setValueAt(subtotal, i, 7);
                        }
                        }
                    }else{
                        noItem = noItem + 1;
                        //"No","Kode","Nama Barang","Harga(Rp)","Bal","Slop","Bks","Total(Rp)"
                        String[] dataField = {Integer.toString(noItem), txtKdBrg.getText(), txtNamaBrg.getText(),
                        txtHarga.getText(),txtBal.getText(),txtSlop.getText(),txtBks.getText(),
                        Integer.toString(subtotal)};
                        DftTabModeTr.addRow(dataField);
                    }
                    
                    int widths[] = {30, 120, 170, 100, 40, 40, 40, 100};
                    for(int i=0;i<tbTransaksi.getColumnCount();i++){
                        TableColumn column = tbTransaksi.getColumnModel().getColumn(i);
                        column.setPreferredWidth(widths[i]);
                    }

                    hitungHTotal();
                    
                    //4 STOK
                    aBal[noItem] = Integer.parseInt(txtBalDB.getText());
                    aSlop[noItem] = Integer.parseInt(txtSlopDB.getText());
                    aBks[noItem] = Integer.parseInt(txtBksDB.getText());                                      

                    txtHargaAkhir.setText(toRp(tHarga));
                    

                    kosongkan();
                    btnTrOk.setEnabled(true);
                    //*/
            }else{
                JOptionPane.showMessageDialog(this, "Data tidak valid, " +
                        "Bal, Slop, atau Bungkus harus diisi atau stok barang kurang","Error",
                    JOptionPane.ERROR_MESSAGE);
            }
            }else{
                System.out.println("Incorrect Number");
                JOptionPane.showMessageDialog(this, "Data tidak valid, " +
                        "Bal, Slop, atau Bungkus harus diisi Angka","Error",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private boolean isAvailable(String kd, Integer i, Integer jum){
        Integer bal = 0, slop = 0, bks = 0;
        
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg WHERE kdBrg='" + kd + "'");            
            while (rslt.next()) {
                bal = rslt.getInt("bal");
                slop = rslt.getInt("slop");
                bks = rslt.getInt("bks");                
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        if (i == 1){
            if (jum > bal){
                return false;
            }else{
                return true;
            }
        }else if(i == 2){
            if (jum > slop){
                return false;
            }else{
                return true;
            }
        }else{
            if (jum > bks){
                return false;
            }else{
                return true;
            }
        }
    }

    public static boolean validateNumber(String num) {
        try {
            Integer.parseInt(num);
            return true;
        } catch (Exception e) {
            return false;
        }
       /*
     * if(validateNumber(num)){
            System.out.println("Correct Number");
        }else{
            System.out.println("Incorrect Number");
        }
     */
    }

    private Date getDateTime(){
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        return date;
        //return dateFormat.format(date);
    }

    private void hitungTxtSubTotal(){
        if(validateNumber(txtBal.getText()) &&
           validateNumber(txtSlop.getText()) &&
           validateNumber(txtBks.getText())){

           Integer iSub = 0;
           iSub = (Integer.parseInt(txtBal.getText()) * 200 * Integer.parseInt(txtHarga.getText()) ) +
                  (Integer.parseInt(txtSlop.getText()) * 20 * Integer.parseInt(txtHarga.getText()) ) +
                  (Integer.parseInt(txtBks.getText()) * Integer.parseInt(txtHarga.getText()) );

           txtSubtotal.setText(toRp(iSub));
        }else{
            txtSubtotal.setText("0");
        }
    }

    //format keluaran currency rupiah
    private String toRp(Integer iHarga){        
        NumberFormat RupiahFormat = NumberFormat.getCurrencyInstance(new Locale("id", "ID"));
        return RupiahFormat.format(iHarga);
    }

    private void hitungHTotal(){
        int rows = tbTransaksi.getRowCount();
        tHarga = 0; //buat lagi ke nol, soalnya diSum di tabel transaksi
        for (int i=0; i< rows; i++) {
            tHarga = tHarga + Integer.parseInt(tbTransaksi.getValueAt(i, 7).toString());
        }
    }
    private boolean isSameKdBrg(String x){
        int rows = tbTransaksi.getRowCount();
        boolean bKetemu = false;
        //tHarga = 0; //buat lagi ke nol, soalnya diSum di tabel transaksi
        for (int i=0; i< rows; i++) {
            //tHarga = tHarga + Integer.parseInt(tbTransaksi.getValueAt(i, 7).toString());
            if(x.equalsIgnoreCase(tbTransaksi.getValueAt(i, 1).toString())){
                bKetemu = true;
            }
        }
        return bKetemu;
    }
    
    
}
